/**
 * Aidttech.com Inc.
 * Copyright (c) 2017-2024 All Rights Reserved.
 */
package com.yunqiu.dbs.service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Lists;
import com.symlich.gme.common.CollectionUtil;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author yunqiu
 * Created by on 2024-07-22 5:39 下午
 */
public abstract class DBService {

    private JdbcTemplate jdbcTemplate;

    public DBService(String ip,String port,String dbName,String userName,String pwd){
        DruidDataSource dataSource = new DruidDataSource();
        // 设置数据源属性参数
        dataSource.setPassword(pwd);
        dataSource.setUrl(getUrl(ip,port,dbName));
        dataSource.setUsername(userName);
        dataSource.setDriverClassName(getDriver());

        jdbcTemplate=new JdbcTemplate(dataSource);
    }

    @SneakyThrows
    public void execute(){
        if(null == jdbcTemplate){
            System.out.println("请先执行init初始化");
            return;
        }
        byte[] buffer = new byte[1024];

        boolean exist = false;

        while (!exist){
            System.out.print("mysql>>");
            Arrays.fill(buffer,(byte)0);
            System.in.read(buffer);
            String content = getInput(buffer);
            if(StringUtils.isEmpty(content)){
                continue;
            }
            if("exit".equalsIgnoreCase(content)){
                return;
            }
            List<String> lines = Lists.newArrayList(content.split(";"));
            if(CollectionUtil.isNotEmpty(lines)){
                for(String line : lines){
                    executeLine(line.toLowerCase());
                }
            }
        }
    }

    /**
     * 驱动
     * @return
     */
    public abstract String getDriver();

    /**
     * url
     * @param ip
     * @param port
     * @param dbName
     * @return
     */
    public abstract String getUrl(String ip,String port,String dbName);

    private void executeLine(String sql){
        if(sql.startsWith("select") || sql.startsWith("show")){
            List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
            if(CollectionUtil.isEmpty(result)){
                return;
            }
            int i = 0;
            StringBuffer headRow = new StringBuffer();
            for(String key :result.get(0).keySet()){
                i++;
                if (i > 1){
                    headRow.append("\t");
                }
                headRow.append(key);
            }
            System.out.println(headRow);

            for (Map<String, Object> stringObjectMap : result) {
                int j=0;
                StringBuffer row = new StringBuffer();
                for (String columnName : stringObjectMap.keySet()) {
                    j++;
                    if (j > 1){
                        row.append("\t");
                    }
                    row.append(stringObjectMap.get(columnName));
                }
                System.out.println(row);
            }
        }else{
            jdbcTemplate.execute(sql);
            System.out.println("执行1条记录成功");
        }
    }

    @SneakyThrows
    private String getInput(byte[] buffer){
        return new String(buffer,"utf-8").trim();
    }
}